package cn.school.mybatis.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import cn.school.mybatis.pojo.Student;

public class TestStudent {

	// 条件查询
	public void searchStu() {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		Map<String, Object> map = new HashMap<>();
		map.put("wechat", "w");
		map.put("qq", "6");
		map.put("sname", "李");

		List<Student> list = studentDao.searchStu(map);
		MyBatisUtil.close(session);
		for (Student s : list) {
			System.out.print(s);
		}
	}

	// 条件查询人数
	public void searchStuCount() {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		Map<String, Object> map = new HashMap<>();
		map.put("wechat", "w");
		map.put("qq", "6");
		map.put("sname", "李");

		int count = studentDao.searchStuCount(map);
		System.out.println("满足条件的共有" + count + "人");

		MyBatisUtil.close(session);

	}

	// 查询全部
	public void searchAll() {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		List<Student> list = studentDao.searchAll();
		MyBatisUtil.close(session);
		for (Student s : list) {
			System.out.print(s);
		}

	}

	// 查询数量
	public void searchCount() {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int count = studentDao.searchCount();
		MyBatisUtil.close(session);
		System.out.println("共有" + count + "条记录");
	}

	// 查询单个
	public void getOne(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		Student result = studentDao.get(num);
		System.out.println(result);
		MyBatisUtil.close(session);
	}

	// 分页查询
	public void searchLimit(int pageNo, int pageSize) {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int offset = (pageNo - 1) * pageSize;
		offset = Math.max(offset, 0);
		List<Student> list = studentDao.searchLimit(offset, pageSize);
		MyBatisUtil.close(session);
		for (Student s : list) {
			System.out.print(s);
		}
	}

	// 修改
	public void update(Student s) {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int result = studentDao.update(s);
		session.commit();
		System.out.println(result + "条修改成功！");
		MyBatisUtil.close(session);
	}

	// 删除
	public void del(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int result = studentDao.del(num);
		session.commit();
		System.out.println(result + "条删除成功！");
		MyBatisUtil.close(session);

	}

	// 增加
	public void add(Student s) {
		SqlSession session = MyBatisUtil.getSqlSession();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int result = studentDao.add(s);
		session.commit();
		System.out.println(result + "条增加成功！");
		MyBatisUtil.close(session);

	}

	public static void main(String[] args) {
		TestStudent tg = new TestStudent();
		// 条件查询
		// tg.searchStu();
		// 条件查询人数
		// tg.searchStuCount();
		// 查询全部
		tg.searchAll();
		// 查询数量
		// tg.searchCount();
		// 查询单个
		// tg.getOne(145);
		// 增加
//		tg.add(new Student("李狗蛋1", "男", "1234567", "233333", "ligoudan1", 3, "2020-07-04", "巢湖", "2020-07-05", "合肥包河"));
		// 修改
		// tg.update(new Student(227, "李狗蛋-1", "男", "1234567227", "233333227",
		// "ligoudan1227", 3, "2020-07-04", "巢湖",
		// "2020-07-05", "合肥包河"));
		// 删除
		// tg.del(226);
		// 分页查询
		// tg.searchLimit(2, 5);

	}

}
